const utils = {
  tableItemToNumber (data, arr) {
    data.forEach(value => {
      for (let key in value) {
        if (arr.indexOf(key) > -1) {
          value[key] = +value[key]
        }
      }
    })
    return data
  },
  cleanObj (val) {
    for (let key in val) {
      if (typeof val[key] === 'string') {
        val[key] = ''
      } else if (typeof val[key] === 'number') {
        val[key] = 0
      } else if (val[key] && typeof val[key] === 'object' && !utils.isArrayFn(val[key])) {
        val[key] = {}
      } else if (utils.isArrayFn(val[key])) {
        val[key] = []
      } else if (val[key] === null) {
        val[key] = []
      }
    }
  },
  isArrayFn (value) {
    if (typeof Array.isArray === 'function') {
      return Array.isArray(value)
    } else {
      return Object.prototype.toString.call(value) === '[object Array]'
    }
  },
  getQueryVariable (variable) {
    var query = window.location.search.substring(1)
    var vars = query.split('&')
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split('=')
      if (pair[0] === variable) {
        return pair[1]
      }
    }
    return (false)
  },

  /* JSON转url参数 */
  jtup: function jtup (data) { // jsonToUrlparams
    return Object.keys(data).map(function (key) {
      return encodeURIComponent(key) + '=' + encodeURIComponent(data[key])
    }).join('&')
  },

  /* 格式化时间戳 */
  aboutTime: function aboutTime (data, stringList) {
    for (let i = 0; i < data.length; i++) {
      for (let j = 0; j < stringList.length; j++) {
        data[i][stringList[j]] = utils.formatDate(data[i][stringList[j]])
      }
    }
  },
  formatDate: function formatDate (now) {
    now = new Date(now)
    var year = now.getFullYear()
    var month = now.getMonth() + 1
    var date = now.getDate()
    var hour = now.getHours()
    var minute = now.getMinutes()
    var second = now.getSeconds()
    if (month < 10) {
      month = '0' + month
    }
    if (date < 10) {
      date = '0' + date
    }
    if (hour < 10) {
      hour = '0' + hour
    }
    if (minute < 10) {
      minute = '0' + minute
    }
    if (second < 10) {
      second = '0' + second
    }
    return year + '-' + month + '-' + date + ' ' + hour + ':' + minute + ':' + second
  }
}

export default utils
